<?php

namespace App\Http\Controllers\Guard\User;

use App\Exports\LiPeiExport;
use App\Http\Controllers\Common\GuardController;
use App\Imports\LiPeiImport;
use App\Models\Rbac\GuardUser;
use App\Models\User\LiPei;
use App\Models\User\LiPeiSum;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;

class LiPeiController extends GuardController
{
    public function index(Request $request) {
        $conditions = $request->input();

        $conditions['orderBy']['col'] = 'created_at';
        $conditions['orderBy']['sort'] = 'desc';

        $records = (new LiPei())->getLst($conditions)
            ->with('getGuardUser')
            ->paginate($conditions['pageSize'],['*'],'page',$conditions['page']);

        return responseSuccess($records->items(),$records->total());
    }

    public function detail(Request $request) {
        $case_number = $request->input('id',0);

        $record = LiPei::where(compact('case_number'))->first();
        return responseSuccess($record->toArray());
    }

    public function change(Request $request) {
        $case_number = $request->input('case_number',0);
        $info = $request->input();
       // dd($info);
        LiPei::where(compact('case_number'))->update($info);
        return responseSuccess();
    }

    public function uploadLiPei(Request $request) {

        $token = $request->input('token');
        $id = checkJWT($token)['id'];
        $guardUser = GuardUser::where(compact('id'))->first();
        //Log::info('upload',compact('guardUser'));
        if(empty($guardUser)){
            return responseFail('当前用户无效！');
        }

        $file = $request->file('file');
        if(empty($file) || !$file->isValid()){
            return responseFail('文件无效！');
        }
        $path = $file->store('user','upload');
        $import = new LiPeiImport($guardUser->id);
        Excel::import($import, storage_path('app/public/upload/'.$path));
        return responseSuccess();
    }

    public function sumIndex(Request $request) {
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',20);
        $conditions = $request->input();
        $records = (new LiPeiSum())->getLst($conditions)
            ->whereHas('getGuardUser', function ($q) use ($conditions) {
                if(!empty($conditions['account'])){
                    $q->where('name','like','%'.$conditions['account'].'%');
                }
            })
            ->with('getGuardUser')->paginate($pageSize,$this->getCol,'page',$page);
        return responseSuccess($records->items(),$records->total());
    }

    public function del(Request $request) {

        $token = $request->header('token');
        $id = checkJWT($token)['id'];

        $case_number = $request->input('case_number',0);
        //LiPei::where(compact('case_number'))->forceDelete();
        try{
            DB::beginTransaction();
            $inst = LiPei::where(compact('case_number'))->first();

            LiPeiSum::create(['account'=>$id,'import_number'=>$inst->import_number,'records_count'=>-1]);
            $inst->forceDelete();

            DB::commit();
            return responseSuccess();
        }catch(\Exception $e){
            DB::rollback();
            $tip = $e->getFile().':'.$e->getLine().':'.$e->getMessage();
            Log::error($tip);
            return responseFail();
        }

    }


    public function exportFile(Request $request) {
        $conditions = $request->input();
        $conditions['orderBy']['col'] = 'created_at';
        $conditions['orderBy']['sort'] = 'desc';
        return Excel::download(new LiPeiExport($conditions),'理赔清单.xlsx');

    }
}
